package cn.hbads.test.context;

import cn.hbads.test.task.ExecuteTask;
import lombok.Getter;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <b> 任务执行上下文 </b>
 *
 * @author : yvvb
 * @date : 2025/5/11
 */
@Getter
public class TaskContext {

    private final Map<String, Object> params = new ConcurrentHashMap<>();
    private final Map<String, ExecuteTask> taskMap;
    private final String batchNo;

    public TaskContext(String batchNo, Map<String, ExecuteTask> taskMap) {
        this.batchNo = batchNo;
        this.taskMap = taskMap;
    }

    public Optional<Object> getParam(String key) {
        return Optional.ofNullable(this.params.get(key));
    }

    public <T> Optional<T> getParam(String key, Class<T> expectType) {
        final var value = params.get(key);
        if (Objects.nonNull(value) && expectType.isAssignableFrom(value.getClass())) {
            return Optional.of((T) value);
        } else {
            return Optional.empty();
        }
    }

    public void put(String key, Object value) {
        params.put(key, value);
    }

    public void putAll(Map<String, Object> properties) {
        params.putAll(properties);
    }
}
